﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

// std headers
#include <map>

// qt headers
#include <QWidget>
#include <QTimer>

// custom headers
#include "GmepGcsMainUI.h"
#include "GbmpNew.h"

class QLabel;
class QFont;

namespace gmepgcs
{
    class GCS_MAINUI_EXPORTS CustomBallon : public QWidget
    {
        Q_OBJECT

    public:
        enum class BallonIcon : int32_t
        {
            NoIcon = 0,
            Info = 1,
            Warning  =2,
            Error = 3,
            Waiting = 4,
            Success = 5,
            Close = 6
        };
        Q_ENUM(BallonIcon)

    public:
        explicit CustomBallon(QWidget *targetWidget);
        virtual ~CustomBallon();

    public:
        template<typename Func>
        static CustomBallon *New(QWidget *targetWidget, const BallonIcon &icon,const std::wstring &message, Func handler);
        static CustomBallon *Get(const QString &key);
        static void Destroy(const QString &key);

    public:
        virtual bool eventFilter(QObject *watched, QEvent *event) override;

    public:
        QString Key();
        CustomBallon *ConfigIcon(const BallonIcon &icon);
        CustomBallon *ConfigMessage(const std::wstring &message);

    protected:
        virtual void paintEvent(QPaintEvent *event) override;

    private:
        void Init();
        CustomBallon *ConfigPosition(const QPoint &pos);
        CustomBallon *ConfigSize(int width, int height);
        CustomBallon *AdjustPosition();

    private:
        static std::map<QString, CustomBallon*> s_mpBallons;

    private:
        QLabel *m_pLblIcon;
        QLabel *m_pLblText;
        QFont m_textFont;

    private:
        QWidget *m_rootWidget;
        QWidget *m_targetWidget;

    private:
        QString m_key;

    };  

    template<typename Func>
    inline CustomBallon *CustomBallon::New(QWidget *targetWidget,const BallonIcon &icon,const std::wstring &message, Func handler)
    {
        auto newBallon = NEW_AS_QT(CustomBallon,targetWidget);
        DBG_WARN_AND_RETURN_UNLESS(newBallon, newBallon, L"newBallon 为空", L"GMEP", L"2024-01-30");
        DBG_WARN_AND_RETURN_UNLESS(newBallon->ConfigIcon(icon), newBallon, L"newBallon->ConfigIcon(icon) 为空", L"GMEP", L"2024-01-30");
        newBallon->ConfigIcon(icon)->ConfigMessage(message);

        CustomBallon::s_mpBallons[newBallon->Key()] = newBallon;

        handler(newBallon);
        return newBallon;
    }
}